#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

void StackInit(ST* ps)
{
	ps->a = (STDataType*)malloc(sizeof(STDataType)*4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	ps->capacity = 4;
	ps->top = 0;
}
void StackDestroy(ST* ps)
{
	assert(ps->a);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)
{
	assert(ps->a);
	if (ps->top == ps->capacity)
	{
		ps->a = (STDataType*)realloc( ps->a ,sizeof(STDataType) * 2 * ps->capacity);
		ps->capacity = ps->capacity * 2;
	}
	ps->a[ps->top] = x;
	ps->top++;

}
void StackPop(ST* ps)
{
	assert(ps->a);
	assert(ps->top > 0);
	ps->top--;
}

STDataType StackTop(ST* ps)
{
	assert(ps->a);
	assert(ps->top >= 0);

	return ps->a[ps->top-1];
}
bool StackEmpty(ST* ps)
{
	return ps->top == 0;
}


int StackSize(ST* ps)
{
	return ps->top;
}

void StackPrintf(ST* ps)
{
	int i = ps->top;
	int j = 0;
	while (i-- > 0)
	{
		printf("%d ", ps->a[j++]);

	}
	printf("\n");
}
